<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp"%>
<%@ include file="/commons/tag.jsp"%>
<%@ include file="/commons/js/basejs.jsp"%>
<%@ include file="/commons/js/extjs.jsp"%>
<%@ include file="/commons/css/basecss.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<link rel="stylesheet" href="${jsPath }/sweetAlert/sweetalert.css" type="text/css"/>

<script type="text/javascript">
	$(function(){
		init_datagrid('${path}');
		$("#addDiv").css("display", "none"); //隐藏添加对话框
		$("#modifyDiv").css("display", "none"); //隐藏修改对话框
	});
	
	//正则表达式验证
	$.extend($.fn.validatebox.defaults.rules, {
		idcard: {// 验证身份证
            validator: function (value) {
                return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);
            },
            message: '身份证号码格式不正确'
        },
	    phoneNum: { //验证手机号   
	        validator: function(value, param){ 
	         return /^1[3-8]+\d{9}$/.test(value);
	        },    
	        message: '请输入正确的手机号码'   
	    }
	});

	//启动表
	function init_datagrid(path) {
		$('#table1').datagrid({
			url: path+'/facUser/selectPage',
			fit: true,
			border: true,
			pagination: true,
			pagePosition: 'bottom',
			idField: 'id',
			pageSize: 10,
			pageList: [10,20,30,40,50],
			checkOnSelect: true,
			selectOnCheck: true,
			singleSelect: false,
			nowrap: true, 
			rownumbers: true, //显示行号
			showfooter: true, //显示行尾
			pageNumber: 1,
			fitColumns: true,
			sortName: 'updateTime',	//按字段排序
			sortOrder: 'DESC',		//倒序
			toolbar : '#datagrid_toolbar',
			columns : [[
			 {field : 'id', title :'id', width : 50, checkbox: true},
			 {field : 'status', title:'状态', width : '10%', align : 'center',sortable:true,
				 formatter: function(value,row,index) {
					 if (0 == value) return '未启用';
					 else if (1 == value) return '有效';
					 else if (-1 == value) return '待审核';
					 else return '';
				 }},
			 {field : 'trueName', title:'姓名', width : '10%', align : 'center',sortable:true},
			 {field : 'userName', title:'用户名', width : '10%', align : 'center',sortable:true},
			 {field : 'sex', title:'性别', width : '5%', align : 'center',sortable:true,
				 formatter: function(value,row,index) {
					 if (0 == value) return '女';
					 else if (1 == value) return '男';
					 else return '';
				 }},
			 {field : 'idCard', title:'身份证号', width : '15%', align : 'center',sortable:true},
			 {field : 'phone', title:'手机', width : '10%', align : 'center',sortable:true},
			 {field : 'facName', title:'厂商名称', width : '20%', align : 'center',sortable:true},
			 {field : 'dept', title:'部门', width : '10%', align : 'center',sortable:true},
			 {field :'action', title:'操作', width: '10%', align:'center',
					formatter:function(value,row,index){
						var e = '<a href="#" onclick="showModifyFrm();">编辑</a>';
						return e;
					}
				}
		   ]] 
	   });
	};
	
	//启动加载角色数据表
	function init_roleDetail(facId) {
		$('#roleDetail').datagrid({
			url: '${path}/systemRole/selectPage',
			fit: true,
			border: false,
			pagination: true,
			pagePosition: 'bottom',
			idField: 'id',
			pageSize: 5,
			pageList: [5,10,15,50],
			checkOnSelect: true,
			selectOnCheck: true,
			singleSelect: false,
			nowrap: true, 
			rownumbers: true, //显示行号
			showfooter: true, //显示行尾
			pageNumber: 1,
			fitColumns:true,
			queryParams: {
				facId: facId
			},
			columns : [[
			 {field : 'id', title :'id', width : 50, checkbox: true},
			 {field : 'roleName', title:'角色名', width : '100%', align : 'center'}
			]]
		});
	}
	
	//启动加载区域数据表
	function init_areaDetail(facId) {
		$('#roleDetail').treegrid({    
		    url:"${path}/agencyArea/findTreeList",    
		    idField:'id',    
		    treeField:'text',		    
		    checkOnSelect: true,
			selectOnCheck: true,
			singleSelect: true,
		    nowrap: true, 
		    fitColumns:true,
		    rownumbers: true, //显示行号
			showfooter: true,//显示行尾
		    columns:[[
				{field: 'id', title: 'id', width: 50, checkbox: true},
		        {field: 'name', title: '区域名称', width: '100%', align : 'center'}    		       
		    ]]
	    	/* onSelect: function (rowIndex, rowData) {
	   	        if (!IsCheckFlag && index != rowIndex) {
	   	        	index=rowIndex;
	   	            IsCheckFlag = true;
	   	        }else if(IsCheckFlag && index != rowIndex){
	   	        	index=rowIndex;
	   	            IsCheckFlag = true;
	   	        }else if(!IsCheckFlag && index == rowIndex){
	   	        	index=rowIndex;
	   	            IsCheckFlag = true;
	   	        }else{
	   	        	$("#tt"+temp).datagrid("clearSelections");
	   	        	IsCheckFlag = false;
	   	        }
    	    }    */            
		}); 
	}
	
	//条件查询
	function doSearch() {			
		$('#table1').datagrid('load',{
			keyword: $('#keyword').val(),
			facId: $('#factory').combobox('getValue'),
			status: $('#status').combobox('getValue')
		});
	}
	
	//显示弹出添加的对话框  
	function showAddFrm() {  
	     $("#addDiv").css("display", "block");
	     $("#addDiv").dialog({  
	         width: 400,  
	         height: 300,
	         modal: true,  
	         title: "添加厂商用户",
	         iconCls: 'icon-add',
	         collapsible: true,  
	         minimizable: true,  
	         maximizable: true,  
	         resizable: true,  
	         buttons: [{  
	             id: 'btnAdd',  
	             text: '保存',  
	             iconCls: 'icon-save',  
	             handler: function () {  		             
	                 $("#addform").submit();  //让表单提交  
	             }  
	         }, {  
	             id: 'btnCancelAdd',  
	             text: '取消',  
	             iconCls: 'icon-cancel',  
	             handler: function () {
	            	 $('#addform').form('clear'); //清除表单数据
	                 $("#addDiv").dialog("close");  
	            }  
	         }]  
	     }); 
 	}
	
	//显示弹出修改的对话框
	function showModifyFrm() {
		var line = $('#table1').datagrid("getSelected");
		if(line == null){
			swal({
				title : '',
				text : '选择需要编辑的条目',
				icon : 'warning',
				timer : 1500,
				buttons : false,
        	})
		} else {
			$("#modifyDiv").css("display", "block");
		    $('#modifyform').form('load',line);
		    $("#modifyDiv").dialog({  
		         width: 400,  
		         height: 300,
		         modal: true,  
		         title: "修改厂商用户",
		         iconCls: 'icon-edit',
		         collapsible: true,  
		         minimizable: true,  
		         maximizable: true,  
		         resizable: true,  
		         buttons: [{  
		             id: 'btnEdit',  
		             text: '保存',  
		             iconCls: 'icon-save',  
		             handler: function () {  		             
		                 $("#modifyform").submit();  //让表单提交  
		             }   
		         }, {  
		             id: 'btnCancelAdd',  
		             text: '取消',  
		             iconCls: 'icon-cancel',  
		             handler: function () {		     
		                 $("#modifyDiv").dialog("close");
		                 $('#table1').datagrid("clearSelections");
		             }  
		         }]  
		     }); 
		 }		
	}

	//删除数据  
    function doDelete() {  
	     var rows = $('#table1').datagrid("getSelections");
	     if (rows.length < 1) {  
	    	 swal({
				title : '',
				text : '选择需要删除的条目',
				icon : 'warning',
				timer : 1500,
				buttons : false,
        	 })	    	 
	     } else {
	    	 $.messager.confirm("确认消息", "您确定要删除吗？", function (r) {  
		         if (r) {  //确认删除
		         	var strIds = "";  
		            for (var i = 0; i < rows.length; i++) {  
		                strIds += rows[i].id + ",";  
		            }  	      
		            ids = strIds.substr(0, strIds.length - 1);
		            $.ajax({
		            	type: "GET",
		            	url: "${path}/facUser/delBy",
		            	data: {"ids":ids},
		            	async: false,  
		                cache: false, 
		            	dataType: "json", 
		                success:function(data){
			                if (data.success) {
			                    swal({
			        				title : '',
			        				text : data.message,
			        				icon : 'success',
			        				timer : 1500,
			        				buttons : false
			                	})			                	
			                    $('#table1').datagrid("reload");  //刷新表格
			                    $('#table1').datagrid("clearSelections");  //去掉选中状态的那些行 			              
			                } else {  
			                    swal({
			        				title : '',
			        				text : data.message,
			        				icon : 'error',
			        				timer : 1500,
			        				buttons : false
			                	}) 
			                } 
		                }
		           });
		        } else {
		        	$('#table1').datagrid("reload");
                    $('#table1').datagrid("clearSelections");
		        }  
	     	});
	    }	 
    }
	
	$(function(){
		 //控制addform表单的提交
		 $('#addform').form({    
		    url: '${path}/facUser/insert',
		    success:function(data){   
		    	var json = JSON.parse(data);
		    	if(json.success){
		    		swal({
						title : '',
						text : json.message,
						icon : 'success',
						timer : 1500,
						buttons : false,
		        	})
		        	$("#addDiv").dialog("close");  //关闭对话框   		         
			        $('#table1').datagrid("reload");  //刷新表 
			        $('#addform').form('clear'); //清除表单数据 */
		        }else{
		        	swal({
						title : '',
						text : json.message,
						icon : 'error',
						timer : 1500,
						buttons : false,
		        	})
		        }   
		    }    
		 });
		 //控制modifyform表单的提交
		 $('#modifyform').form({    
		    url: '${path}/facUser/update',
		    success:function(data){
		    	var json = JSON.parse(data);
		        if(json.success){
		        	swal({
						title : '',
						text : json.message,
						icon : 'success',
						timer : 1500,
						buttons : false,
		        	})
		        	$("#modifyDiv").dialog("close");  //关闭对话框   		         
			        $('#table1').datagrid("reload");  //刷新表
			        $('#table1').datagrid("clearSelections");
		        }else{
		        	swal({
						title : '',
						text : json.message,
						icon : 'error',
						timer : 1500,
						buttons : false,
		        	})
		        }   
		    }    
		});
	});
	
	//角色授权
	function roleAutho() {
		var line = $('#table1').datagrid("getSelected");
		if( line == null ){
			swal({
				title : '',
				text : '选择需要授权的用户',
				icon : 'warning',
				timer : 1500,
				buttons : false
	        })
		} else {
			$("#roleDiv").dialog({
				width: 400,  
		        height: 300,
		        modal: true,  
		        title: "角色授权",
		        iconCls: 'icon-save',
		        collapsible: true,  
		        minimizable: true,  
		        maximizable: true,  
		        resizable: true,
		        buttons: [{  
		             id: 'button',  
		             text: '保存',  
		             iconCls: 'icon-save',  
		             handler: function () {
		            	var flag = $('#roleDetail').datagrid("getSelected");
		            	if( flag == null ){
		            		swal({
		        				title : '',
		        				text : '请选择角色',
		        				icon : 'warning',
		        				timer : 1500,
		        				buttons : false
		        	        })
		            	} else {
		            		shouquan(flag,line);
		            	}
		            }
		         }, {  
		             id: 'btnCancelAdd',  
		             text: '取消',  
		             iconCls: 'icon-cancel',  
		             handler: function () {		     
		                 $("#roleDiv").dialog("close");
		                 $('#roleDetail').datagrid("clearSelections");
			       		 $('#table1').datagrid("clearSelections");
		             }  
		         }]  
			});
			init_roleDetail(line.facId);	
		}
	}
	
	//区域授权
	function areaAutho() {
		var line = $('#table1').datagrid("getSelected");
		if( line == null ){
			swal({
				title : '',
				text : '选择需要授权的用户',
				icon : 'warning',
				timer : 1500,
				buttons : false
	        })
		} else {			
			$("#areaDiv").dialog({
				width: 400,  
		        height: 300,
		        title: "区域授权",
		        iconCls: 'icon-save',
		        buttons: [{  
		             id: 'button',  
		             text: '保存',  
		             iconCls: 'icon-save',  
		             handler: function () {
		            	var flag = $('#areaDetail').datagrid("getSelected");
		            	if( flag == null ){
		            		swal({
		        				title : '',
		        				text : '请选择区域',
		        				icon : 'warning',
		        				timer : 1500,
		        				buttons : false
		        	        })
		            	} else {
		            		//区域授权核心代码块
		            	}
		            }
		         }, {  
		             id: 'btnCancelAdd',  
		             text: '取消',  
		             iconCls: 'icon-cancel',  
		             handler: function () {		     
		                 $("#areaDiv").dialog("close");
		                 $('#areaDetail').datagrid("clearSelections");
			       		 $('#table1').datagrid("clearSelections");
		             }  
		         }]  
			});
			init_areaDetail(line.facId);
		}
	}
	
	function shouquan(flag,line) {
		$.ajax({
   		 url:"${path}/facUser/roleAutho",
   		 type:"GET",
   		 dataType:"json",
   		 async:true,
   		 data:{
   			 "roleId":flag.id,
   		 	 "userId":line.id
   		 }, 
       	 success:function(data){		            		
     		 if(data.success){
	   			 swal({
					title : '',
					text : data.message,
					icon : 'success',
					timer : 1500,
					buttons : false
			     })
	       		 $("#roleDiv").dialog("close");
	       		 $('#roleDetail').datagrid("clearSelections");
	       		 $('#table1').datagrid("clearSelections");
     		 } else {
       			 swal({
					title : '',
					text : data.message,
					icon : 'error',
					timer : 1500,
					buttons : false
   		         })
   		      	 $('#roleDetail').datagrid("clearSelections");
       		 }
		 }
 		});	
	}
	
	// 密码重置
	function resetPsd() {
		var rows = $('#table1').datagrid("getSelections");
		if( rows.length < 1 ){
			swal({
				title : '',
				text : '选择需要重置密码的用户',
				icon : 'warning',
				timer : 1500,
				buttons : false
	        })
		} else {
			var strIds = "";  
            for (var i = 0; i < rows.length; i++) {  
                strIds += rows[i].id + ",";  
            }  	      
            ids = strIds.substr(0, strIds.length - 1);
			$.ajax({
				url:'${path}/facUser/resetPsd',
				type:'GET',
				data:{'ids':ids},
				dataType:'JSON',
				async:true,
				success:function(data){
					if( data.success ){
						swal({
							title : '',
							text : data.message,
							icon : 'success',
							timer : 1500,
							buttons : false
				        })
					} else {
						swal({
							title : '',
							text : data.message,
							icon : 'error',
							timer : 1500,
							buttons : false
				        })
					}
					$('#table1').datagrid("clearSelections");					
				}
			});
		}
	}
	
</script>

	
<div class="easyui-layout" data-options="fit:true,border:false">

	<!-- 顶部条件查询、模糊查询 -->
    <div data-options="region:'north',border:false" class="top_sreach" style="height:35px">
    	<table>
    		<tr>
    			<td>关键字：</td>
    			<td><input id="keyword" name="keyword" class="text" placeholder="用户名/真实姓名" /></td>
    			<td>请选择厂商：</td>
    			<td><input id="factory" name="factory" class="easyui-combobox" type="text"  
					data-options="valueField:'label',textField:'value',url:'${base}/SystemProduct/findAllFactory'"/></td>
				<td>
					<select id="status" name="status" class="easyui-combobox" style="width:100px;" editable=false panelHeight=auto>
						<option value="" >请选择状态</option>
						<option value="0" >未启用</option>
					    <option value="1">有效</option>   
					    <option value="-1">待审核</option>     
					</select>
				</td>
				<td>
					<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="doSearch()">搜索</a>
			    </td>
    		</tr>
    	</table>
    </div>

	<!-- 主数据表 -->
	<div data-options="region:'center',border:false" style="width: 100%;">
		<table id="table1"></table>
	</div>

    <!-- 角色数据表 -->
    <div id="roleDiv" >
    	<table id="roleDetail"></table>
    </div>
    
    <!-- 区域数据表 -->
    <div id="areaDiv" >
    	<table id="areaDetail"></table>
    </div>

	<!-- 菜单按钮 -->
    <div id="datagrid_toolbar">
		<a onclick="showAddFrm();" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">添加</a>
		<a onclick="showModifyFrm();" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">编辑</a>
        <a onclick=";" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search',disabled:true">查看</a>
       	<a onclick="doDelete();" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'">删除</a>
       	<a onclick="roleAutho();" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'">角色授权</a>
       	<a onclick="areaAutho();" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'">区域授权</a>
       	<a onclick="resetPsd();" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-reload'">密码重置</a>
	</div>
	
	<!-- 新增表单 -->
	<div id="addDiv">  
    	<form id="addform" method="post">
         	<table>
         		<tr>
         			<td><label for="facId">厂商</label></td>
         			<td>：</td>
         			<td>
         				<input name="facId" class="easyui-combobox" type="text" 
         					data-options="required:true,valueField:'label',textField:'value',url:'${base}/SystemProduct/findAllFactory'"/>
         			</td>
         		</tr>
         		<tr>
         			<td><label for="userName">用户名</label></td>
         			<td>：</td>
         			<td><input class="easyui-textbox" type="text" name="userName" data-options="required:true" /></td>
         		</tr>
         		<tr>
         			<td><label for="trueName">真实姓名</label></td>
         			<td>：</td>
         			<td><input class="easyui-textbox" type="text" name="trueName" data-options="required:false" /></td>
         		</tr>
         		<tr>
         			<td><label for="sex">性别</label></td>
         			<td>：</td>
         			<td>
         				<select class="easyui-combobox" name="sex" data-options="limitToList:true">
         					<option value="1">男</option>
         					<option value="0">女</option>
         				</select>
         			</td>
         		</tr>
         		<tr>
         			<td><label for="idCard">身份证号</label></td>
         			<td>：</td>
         			<td><input class="easyui-textbox" type="text" name="idCard" 
         							data-options="required:false, validType:'idcard'" /></td>
         		</tr>
         		<tr>
         			<td><label for="phone">手机号码</label></td>
         			<td>：</td>
         			<td><input class="easyui-textbox" type="text" name="phone" 
         							data-options="required:false, validType:'phoneNum'" /></td>
         		</tr>
         		<tr>
         			<td><label for="dept">部门</label></td>
         			<td>：</td>
         			<td><input class="easyui-textbox" type="text" name="dept" data-options="required:false" /></td>
         		</tr>
         	</table>
		</form> 
    </div>
    
    
    <!-- 修改表单 -->
	<div id="modifyDiv">  
    	<form id="modifyform" method="post">
    		<input class="easyui-validatebox" type="text" name="id" data-options="required:true" hidden="true"/>
         	<table>
         		<tr>
         			<td><label for="facId">厂商</label></td>
         			<td>：</td>
         			<td>
         				<input name="facId" class="easyui-combobox" type="text"
         					data-options="required:true,valueField:'label',textField:'value',url:'${base}/SystemProduct/findAllFactory'"/>
         			</td>
         		</tr>
         		<tr>
         			<td><label for="status">状态</label></td>
         			<td>：</td>
         			<td>
         				<select class="easyui-combobox" name="status" data-options="limitToList:true">
         					<option value="1">有效</option>
         					<option value="0">未启用</option>
         					<option value="-1">待审核</option>
         				</select>
         			</td>
         		</tr>
         		<tr>
         			<td><label for="userName">用户名</label></td>
         			<td>：</td>
         			<td><input class="easyui-textbox" type="text" name="userName" data-options="required:true" /></td>
         		</tr>
         		<tr>
         			<td><label for="trueName">真实姓名</label></td>
         			<td>：</td>
         			<td><input class="easyui-textbox" type="text" name="trueName" data-options="required:false" /></td>
         		</tr>
         		<tr>
         			<td><label for="sex">性别</label></td>
         			<td>：</td>
         			<td>
         				<select class="easyui-combobox" name="sex" data-options="limitToList:true">
         					<option value="1" selected="selected">男</option>
         					<option value="0">女</option>
         				</select>
         			</td>
         		</tr>
         		<tr>
         			<td><label for="idCard">身份证号</label></td>
         			<td>：</td>
         			<td><input class="easyui-textbox" type="text" name="idCard" 
         							data-options="required:false, validType:'idcard'" /></td>
         		</tr>
         		<tr>
         			<td><label for="phone">手机号码</label></td>
         			<td>：</td>
         			<td><input class="easyui-textbox" type="text" name="phone" 
         							data-options="required:false, validType:'phoneNum'" /></td>
         		</tr>
         		<tr>
         			<td><label for="dept">部门</label></td>
         			<td>：</td>
         			<td><input class="easyui-textbox" type="text" name="dept" data-options="required:false" /></td>
         		</tr>
         	</table>
		</form> 
    </div>

</div>

